package com.blb.controller;

import com.blb.pojo.Department;
import com.blb.pojo.JsonResult;
import com.blb.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//每个方法都加@ResponseBody
//@CrossOrigin
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/departments")
    public JsonResult findAll(){
        List<Department> departments = departmentService.findAll();
        return new JsonResult(200L,departments);
    }

    @GetMapping("/department/{id}")
    public JsonResult findById(@PathVariable Long id){
        Department depart = departmentService.findById(id);
        return new JsonResult(200L,depart);
    }

    //@RequestBody 用于接收json格式的参数
    @PostMapping("/department")
    public JsonResult addDepartment(@RequestBody Department department){
        departmentService.saveDepartment(department);
        return new JsonResult(200L,department);
    }

    //@RequestBody 用于接收json格式的参数
    //没有@RequestBody 需要使用表单格式 id=1&name=xx&address=xxxx
    @PutMapping("/department")
    public JsonResult updateDepartment(@RequestBody Department department){
        departmentService.updateDepartment(department);
        return new JsonResult(200L,department);
    }

    //@RequestBody 用于接收json格式的参数
    @DeleteMapping("/department/{id}")
    public JsonResult addDepartment(@PathVariable Long id){
        departmentService.deleteDepartment(id);
        return new JsonResult(200L,id);
    }
}
